import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    // 组件的状态统一放到state中
    count: 0,
    msg: 'hello',
    list: []
  },
  mutations: {
    // 用于修改count数据
    updateCount (state, payload) {
      state.count = state.count + payload
    },
    updateMsg (state, payload) {
      state.msg = payload
    },
    initList (state, payload) {
      state.list = payload
    }
  },
  actions: {
    async queryData (context, payload) {
      // context 类似于 $store
      const ret = await axios.get('http://test.zjie.wang/tab')
      context.commit('initList', ret.data.list)
    }
  },
  getters: {
    newMsg (state) {
      return '<' + state.msg + '>'
    }
  }
})
